package com.hk.servlet;

import com.alibaba.fastjson.JSONObject;
import com.hk.model.ContactUs;
import com.hk.model.vo.ResultVO;
import com.hk.service.IContactUsService;
import com.hk.service.impl.ContactUsServiceImpl;
import com.hk.utils.CharacterEncoding;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

/**
 * @Author: KangWang
 * Date: 2021/5/20 17:35
 * State：
 */
@WebServlet("/contactUs")
public class ContactUsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        统一字符编码
        CharacterEncoding.setCharacterEncodingResp(req,resp,"UTF-8");
        IContactUsService iContactUsService = new ContactUsServiceImpl();
        List<ContactUs> contactUsList = iContactUsService.findAll(-1, -1);
        ResultVO<ContactUs> resultVO = new ResultVO<ContactUs>();
        PrintWriter out = resp.getWriter();
        if (!contactUsList.isEmpty()) {
            resultVO.setStatus(1);
            resultVO.setMessage("success");
            resultVO.setTotal(contactUsList.size());
            resultVO.setList(contactUsList);
        } else {
            resultVO.setStatus(0);
            resultVO.setMessage("error");
            resultVO.setTotal(contactUsList.size());
            resultVO.setList(contactUsList);
        }
        String resultVOJSON = JSONObject.toJSONString(resultVO);
        out.write(resultVOJSON);
        out.flush();
        out.close();


    }

}
